﻿#include "stdafx.h"
#include "DetectCycle.h"

using namespace std;

namespace algorithm {
ListNode* CDetectCycle::First(ListNode* head)
{
    unordered_set<long> cache;
    ListNode* pNode = head;
    while (pNode) {
        auto it = cache.find((long)pNode);
        if (it != cache.end()) {
            return pNode;
        }
        cache.insert((long)pNode);
        pNode = pNode->next;
    }
    return nullptr;
}

}
